package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;

import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
            //清空redis数据库
            jedis.flushAll();
            //testLpushAndLpop(jedis);
            //testRpushAndRpop(jedis);
            //testLrange(jedis);
            //testBLpop(jedis);
            //testBRpop(jedis);
            //testLindex(jedis);
            //testLlen(jedis);
        }
    }
    public static void testLpushAndLpop(Jedis jedis) {
        System.out.println("测试 lpush 和 lpop");
        long n = jedis.lpush("key", "1", "2", "3", "4");
        System.out.println(n);
        String value = jedis.lpop("key");
        System.out.println(value);
        value = jedis.lpop("key");
        System.out.println(value);
        value = jedis.lpop("key");
        System.out.println(value);
        value = jedis.lpop("key");
        System.out.println(value);
        value = jedis.lpop("key");
        System.out.println(value);
    }
    private static void testRpushAndRpop(Jedis jedis) {
        System.out.println("测试 rpush 与 rpop");
        long n = jedis.rpush("key", "1", "2", "3", "4");
        System.out.println(n);
        String value = jedis.rpop("key");
        System.out.println(value);
        value = jedis.rpop("key");
        System.out.println(value);
        value = jedis.rpop("key");
        System.out.println(value);
        value = jedis.rpop("key");
        System.out.println(value);
        value = jedis.rpop("key");
        System.out.println(value);
    }
    private static void testLrange(Jedis jedis) {
        System.out.println("测试 lrange ");
        jedis.rpush("key", "1", "2", "3", "4");
        List<String> values = jedis.lrange("key", 1, 3);
        System.out.println(values);
    }
    private static void testBLpop(Jedis jedis) {
        System.out.println("测试 bloop");
        while (true) {
            List<String> values = jedis.blpop(0, "key");
            System.out.println(values);
        }
    }
    private static void testBRpop(Jedis jedis) {
        System.out.println("开始调⽤ brpop");
        while (true) {
            List<String> values = jedis.brpop(0, "key");
            System.out.println(values);
        }
    }
    private static void testLindex(Jedis jedis) {
        System.out.println("测试 lindex");
        jedis.rpush("key", "1", "2", "3", "4");
        String value = jedis.lindex("key", 2);
        System.out.println(value);
    }
    private static void testLinsert(Jedis jedis) {
        jedis.rpush("key", "a", "b", "c", "d");
        jedis.linsert("key", ListPosition.BEFORE, "c", "100");
        List<String> values = jedis.lrange("key", 0, -1);
        System.out.println(values);
    }
    private static void testLlen(Jedis jedis) {
        jedis.rpush("key", "a", "b", "c", "d");
        long n = jedis.llen("key");
        System.out.println(n);
    }
}
